//
//  MessageViewController.m
//  SIPphone
//
//  Created by wrt on 13-10-25.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//

#import "MessageViewController.h"
#import "PostMessageViewController.h"
#import "OnlineMessageViewController.h"
#import "FriendRequestViewController.h"
#import "AlarmMessageViewController.h"
#import "PostMessageDataBase.h"
#import "PromptChatDateBase.h"
#import "FriendRespondDateBase.h"
#import "AlarmDataBase.h"
#import "AppDelegate.h"
#import "MonitorViewController.h"

@interface MessageViewController ()

@end

@implementation MessageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"message", @" 信息 tabbar Item For message name ");
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.titleArray = @[@"推送信息",@"即时信息",@"好友请求信息",@"报警信息"];
    self.titleArray = @[NSLocalizedString(@"push message", @"推送信息 Push message"),NSLocalizedString(@"instant message", @"即时信息 Instant message"),NSLocalizedString(@"alarm message", @"报警消息 Alarm message")];
    
    self.dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:self.titleArray,@"1",@[NSLocalizedString(@"Monitor", @"监视 Monitor")],@"2", nil];
    
    //设置四个通知，若有新消息则刷新对应cell上的未读消息数目
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(haveNewPostMsg) name:@"HAVEANEWPOSTMSG" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(haveNewOnlineMsg) name:@"HAVEANEWONLINEMSG" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(haveNewFriendRequestMsg) name:@"HAVEFRIENDREQUESTMSG" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(haveNewAlarmMsg) name:@"HAVENEWALARMMSG" object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    //获取选择的cell是哪一个
//     [self.tableView reloadData];
    //若均没有未读消息则将tabbar上的new去掉
    int i,j,k,l;
    i = [self haveNewPostMsg];
    j = [self haveNewOnlineMsg];
    k = [self haveNewFriendRequestMsg];
    l = [self haveNewAlarmMsg];
    if ((i + j + k + l) == 0)
    {
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        app.navForMsg.tabBarItem.badgeValue = nil;
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return 4;
//    return 3;
    if (section == 0)
    {
        return 3;
    }
    else if (section == 1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    
    NSString *key = [[self.dictionary allKeys] objectAtIndex:section];
    NSArray *nameSection = [self.dictionary objectForKey:key];
    
    static NSString *cellIdentifier = @"moreIdentifer";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.textLabel.text = nameSection[row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            cell.imageView.image = [UIImage imageNamed:@"msg-tui_32.png"];
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, 24, 24)];
            [textField setBorderStyle:UITextBorderStyleNone];
            [textField setEnabled:NO];
            textField.background = [UIImage imageNamed:@"AlbumNewNotify.png"];
            textField.textColor = [UIColor whiteColor];
            textField.textAlignment = 1;
            textField.contentVerticalAlignment = 0;
            textField.font = [UIFont boldSystemFontOfSize:10];
            PostMessageDataBase *postMsgDB = [PostMessageDataBase sharePostMessageDateBase];
            int i = 0;
            i = [postMsgDB countForUnreadWith];
            if (i == 0)
            {
                [textField setHidden:YES];
            }
            else if (i > 99)
            {
                [textField setHidden:NO];
                textField.text = [NSString stringWithFormat:@"99"];
            }
            else
            {
                [textField setHidden:NO];
                textField.text = [NSString stringWithFormat:@"%d",i];
            }
            textField.tag = 111;
            [cell addSubview:textField];
        }
        if (indexPath.row == 1)
        {
            cell.imageView.image = [UIImage imageNamed:@"msg-hao_64.png"];
            
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, 24, 24)];
            [textField setBorderStyle:UITextBorderStyleNone];
            [textField setEnabled:NO];
            textField.background = [UIImage imageNamed:@"AlbumNewNotify.png"];
            textField.textColor = [UIColor whiteColor];
            textField.textAlignment = 1;
            textField.contentVerticalAlignment = 0;
            textField.font = [UIFont boldSystemFontOfSize:10];
            PromptChatDateBase *promptDB = [PromptChatDateBase sharePromptChatDB];
            int i = 0;
            i = [promptDB countForUnreadWith];
            if (i == 0)
            {
                [textField setHidden:YES];
            }
            else if (i > 99)
            {
                [textField setHidden:NO];
                textField.text = [NSString stringWithFormat:@"99"];
            }
            else
            {
                [textField setHidden:NO];
                textField.text = [NSString stringWithFormat:@"%d",i];
            }
            textField.tag = 112;
            [cell addSubview:textField];
        }
        //    if (indexPath.row == 2)
        //    {
        //        cell.imageView.image = [UIImage imageNamed:@"msg-hao_64.png"];
        //
        //        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, 24, 24)];
        //        [textField setBorderStyle:UITextBorderStyleNone];
        //        [textField setEnabled:NO];
        //        textField.background = [UIImage imageNamed:@"AlbumNewNotify.png"];
        //        textField.textColor = [UIColor whiteColor];
        //        textField.textAlignment = 1;
        //        textField.contentVerticalAlignment = 0;
        //        textField.font = [UIFont boldSystemFontOfSize:10];
        //        FriendRespondDateBase *frdRspdDB = [FriendRespondDateBase shareFriendRespondDateBase];
        //        int i = 0;
        //        i = [frdRspdDB countForUnreadWith];
        //        if (i == 0)
        //        {
        //            [textField setHidden:YES];
        //        }
        //        else if (i > 99)
        //        {
        //            [textField setHidden:NO];
        //            textField.text = [NSString stringWithFormat:@"99"];
        //        }
        //        else
        //        {
        //            [textField setHidden:NO];
        //            textField.text = [NSString stringWithFormat:@"%d",i];
        //        }
        //        textField.tag = 113;
        //        [cell addSubview:textField];
        //    }
        if (indexPath.row == 2)
        {
            cell.imageView.image = [UIImage imageNamed:@"baojingxiaoxi.png"];
            
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, 24, 24)];
            [textField setBorderStyle:UITextBorderStyleNone];
            [textField setEnabled:NO];
            textField.background = [UIImage imageNamed:@"AlbumNewNotify.png"];
            textField.textColor = [UIColor whiteColor];
            textField.textAlignment = 1;
            textField.contentVerticalAlignment = 0;
            textField.font = [UIFont boldSystemFontOfSize:10];
            AlarmDataBase *alarmDB = [AlarmDataBase shareAlarmDataBase];
            int i = 0;
            i = [alarmDB countForUnread];
            if (i == 0)
            {
                [textField setHidden:YES];
            }
            else if (i > 99)
            {
                [textField setHidden:NO];
                textField.text = [NSString stringWithFormat:@"99"];
            }
            else
            {
                [textField setHidden:NO];
                textField.text = [NSString stringWithFormat:@"%d",i];
            }
            textField.tag = 114;
            [cell addSubview:textField];
        }
    }
    else if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            cell.imageView.image = [UIImage imageNamed:@"监视.png"];
        }
    }
    
    return cell;
}

#pragma mark - Table view delegate
//分别选择哪个按钮会跳转到对应的界面上
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.navForMsg.tabBarItem.badgeValue = nil;
    
    if (indexPath.section == 0)
    {
        NSUInteger row=indexPath.row;
        if (row == 0)
        {
            PostMessageViewController *pmVC = [[PostMessageViewController alloc] init];
            [pmVC setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:pmVC animated:YES];
        }
        if (row == 1)
        {
            OnlineMessageViewController *omVC = [[OnlineMessageViewController alloc] init];
            [omVC setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:omVC animated:YES];
        }
        //    if (row == 2)
        //    {
        //        FriendRequestViewController *friendRequstVC = [[FriendRequestViewController alloc] init];
        //        [self.navigationController pushViewController:friendRequstVC animated:YES];
        //    }
        if (row == 2)
        {
            AlarmMessageViewController *amVC = [[AlarmMessageViewController alloc] init];
            [amVC setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:amVC animated:YES];
        }
    }
    else if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            MonitorViewController *monitorVC = [[MonitorViewController alloc] init];
            [monitorVC setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:monitorVC animated:YES];
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (int)haveNewPostMsg
{
    UITextField *textField1 = (UITextField *)[self.view viewWithTag:111];
    PostMessageDataBase *postMsgDB = [PostMessageDataBase sharePostMessageDateBase];
    int i = 0;
    i = [postMsgDB countForUnreadWith];
    dispatch_async(dispatch_get_main_queue(),^{
        if (i == 0)
        {
            [textField1 setHidden:YES];
        }
        else if (i > 99)
        {
            [textField1 setHidden:NO];
            textField1.text = [NSString stringWithFormat:@"99"];
        }
        else
        {
            [textField1 setHidden:NO];
            textField1.text = [NSString stringWithFormat:@"%d",i];
        }
    });
    return i;
}

- (int)haveNewOnlineMsg
{
    UITextField *textField2 = (UITextField *)[self.view viewWithTag:112];
    PromptChatDateBase *promptDB = [PromptChatDateBase sharePromptChatDB];
    int j = 0;
    j = [promptDB countForUnreadWith];
    dispatch_async(dispatch_get_main_queue(),^{
        if (j == 0)
        {
            [textField2 setHidden:YES];
        }
        else if (j > 99)
        {
            [textField2 setHidden:NO];
            textField2.text = [NSString stringWithFormat:@"99"];
        }
        else
        {
            [textField2 setHidden:NO];
            textField2.text = [NSString stringWithFormat:@"%d",j];
        }
    });
    return j;
}

- (int)haveNewFriendRequestMsg
{
    UITextField *textField3 = (UITextField *)[self.view viewWithTag:113];
    FriendRespondDateBase *frdRspdDB = [FriendRespondDateBase shareFriendRespondDateBase];
    int k = 0;
    k = [frdRspdDB countForUnreadWith];
    dispatch_async(dispatch_get_main_queue(),^{
        if (k == 0)
        {
            [textField3 setHidden:YES];
        }
        else if (k > 99)
        {
            [textField3 setHidden:NO];
            textField3.text = [NSString stringWithFormat:@"99"];
        }
        else
        {
            [textField3 setHidden:NO];
            textField3.text = [NSString stringWithFormat:@"%d",k];
        }
    });
    return k;
}

- (int)haveNewAlarmMsg
{
    UITextField *textField4 = (UITextField *)[self.view viewWithTag:114];
    AlarmDataBase *alarmDB = [AlarmDataBase shareAlarmDataBase];
    int k = 0;
    k = [alarmDB countForUnread];
    dispatch_async(dispatch_get_main_queue(),^{
        if (k == 0)
        {
            [textField4 setHidden:YES];
        }
        else if (k > 99)
        {
            [textField4 setHidden:NO];
            textField4.text = [NSString stringWithFormat:@"99"];
        }
        else
        {
            [textField4 setHidden:NO];
            textField4.text = [NSString stringWithFormat:@"%d",k];
        }
    });
    return k;
}

@end
